<template>
  <div>
    <div style="align-content: center;">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="封面">
          <div>
            <el-upload
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <el-input style="display:none" v-model="form.fengmian"></el-input>
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>
        </el-form-item>

        <el-form-item label="分区">
          <el-select v-model="form.region" placeholder="请选择分区">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="内容">
          <el-input type="textarea" v-model="form.desc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">发布专栏</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "uploadartcle",

  data() {
    return {
      imageUrl: "",
      form: {
        title: "",
        region: "",
        fengmian: "",
        resource: "",
        desc: ""
      },
      fileList: [{ name: "666.mp4", url: "" }]
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    onSubmit() {
      alert("投稿喽！");
    },
    cancel() {
      history.back();
    }
  }
};
</script>
<style   scoped>
.avatar-uploader-icon {
  border: dashed 1px #d3d3d3;
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar-uploader{
    width: 178px;
  height: 178px;
}
.avatar-uploader:hover {
  width: 178px;
  height: 178px;
  border: dashed 1px rgb(0, 179, 250);
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
